#include "../inc/myTypes.h"
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>

#define LCD_PATH "/dev/fb0"
#define LCD_WIDTH 800
#define LCD_HEIGHT 480
#define LCD_BPP 4
#define LCD_SIZE ((LCD_HEIGHT) * (LCD_WIDTH) * (LCD_BPP))

lcdInfo *  InitLCD( void )
{

	lcdInfo * lcd = calloc( 1 , sizeof(lcdInfo) );


	// 打开LCD显示器
	lcd->fd = open( LCD_PATH , O_RDWR );
	if (lcd->fd < 0)
	{
		perror( "open lcd error" );
		goto OpenLcdError;  // 无条件跳转到指定的位置执行
		
	}
	

	// 内存映射
	lcd->map = mmap( NULL , LCD_SIZE , PROT_READ | 
						PROT_WRITE , MAP_SHARED ,
						lcd->fd , 0 );
	if (lcd->map == MAP_FAILED)
	{
		perror( "mmap error " );
		goto MMAPError ; // 无条件跳转到指定的位置执行
	}

	lcd->lcdSize = LCD_SIZE ;
	

	// 返回
	return lcd ;

MMAPError:
	close (lcd->fd);
OpenLcdError:
	free(lcd) ;
	return NULL ;
}


int ShowBmp( lcdInfo * lcd , const char * FilePath , int x , int y )
{

	// 打开图像文件
	FILE * fp = fopen( FilePath  , "r");
	if (fp == NULL)
	{
		perror("open file error");
		return -1 ;
	}

	// 读取图像的头部信息（文件头+信息头）
	// 读取文件信息头数据
	BITMAPFILEHEADER fileInfo = {0};
	int retVla = fread(  &fileInfo , 1 ,sizeof(fileInfo) , fp );
	printf("retVal:%d\n", retVla );

	printf("文件大小：%d\n" , fileInfo.bfSize);
	printf("RGB偏移位置: %d\n" , fileInfo.bfOffBits );


	// 读取图像信息头数据
	BITMAPINFOHEADER PicInfo = { 0 } ; 
	retVla = fread(  &PicInfo , 1 ,sizeof(PicInfo) , fp );
	printf("retVal:%d\n", retVla );

	printf("图像宽高[%d,%d]\n" , PicInfo.biWidth , PicInfo.biHeight );
	printf("图像色深[%d]\n" , PicInfo.biBitCount );
	printf("压缩模式[%d]\n" , PicInfo.biCompression );

	// 计算并检测是否存在行尾空白符（为了能被4整除）
	int Whitespace = 0 ;

	if( PicInfo.biWidth * 3 % 4 )
	{
		// 计算的到为了能4整除而补充的空白符数量
		Whitespace = 4 - PicInfo.biWidth * 3 % 4 ;
	}

	// 读取图像的BGR颜色值
	char Bgr [ PicInfo.biHeight * PicInfo.biWidth * 3 +  PicInfo.biHeight * Whitespace ];
	retVla = fread( Bgr , 1 , sizeof(Bgr) , fp );
	printf("读取到图像RGB:%d\n" , retVla );



	// 关闭图像文件
	fclose(fp) ;

	// 把BGR转换为ARGB
	int ARGB [PicInfo.biHeight] [PicInfo.biWidth] ;
	for (int y = 0; y < PicInfo.biHeight ; y++)
	{
		for (int x = 0; x < PicInfo.biWidth ; x++)
		{
			ARGB[PicInfo.biHeight-y-1][x] = 	Bgr[(x+y*PicInfo.biWidth)*3+0 + y*Whitespace] << 0 | 
												Bgr[(x+y*PicInfo.biWidth)*3+1 + y*Whitespace] << 8 | 
												Bgr[(x+y*PicInfo.biWidth)*3+2 + y*Whitespace]<< 16 ;
		}
		
	}



	// 返回
	return 0 ;

}